package com.liangzhong.proxy.shop
{
	import com.common.utils.Debug;
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.shop.CMD_Shop;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 * 商城proxy 
	 * @author Administrator
	 * 
	 */	
	public class ShopProxy extends BaseProxy
	{
		/**
		 *俱乐部proxy名字 
		 */
		public static const PROXYNAME:String = "shop_proxyname";
		public function ShopProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
//			requestShopList();
		}
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case MainCommandType.PUBLICCOMMANDTYPE://公用协议
					
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
//				case MainCommandType.MAINDEAL:
//					return onMainDeal(subCmdID,buffer,dataSize,clientSocket);
			}
			return false;
		}
		/**
		 * 处理
		 * 公共协议类型 40
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */
		private function onDeal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			var results:int;
			switch(subCmdID)
			{
				case CMD_Shop.BUYGOODS:
					getBuyGoods(buffer);
					return true;
			}
			return false;	
		}
		/**
		 *请求商城列表数据 
		 * 
		 */
		public function requestShopList():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.SHOW_INFO_LIST;
			ProxyUtils.instance.webClient(getShopList,variables,false);
		}
		private function getShopList($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,ShopProxy);
			if(obj.retcode == 0)
			{
				var arr:Array = obj.store;
				AppData.getInstance().shopDataManager.analsisList(arr);
			}else
			{
//				Debug.echo("++++++++"+ErrorTips.instance.getErrorTip(obj.msg));
			}
			
		}
		/**
		 *请求购买物品 
		 * @param $obj
		 * 
		 */
		public function requestBuyGoods($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 7,MainCommandType.PUBLICCOMMANDTYPE,CMD_Shop.BUYGOODS);
			var id:int = $obj.id;
			sendBuf.writeInt(id);
			sendBuf.writeByte($obj.type)
			sendBuf.writeShort($obj.num);
			
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回购买物品 
		 * @param buffer
		 * 
		 */
		private function getBuyGoods(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn == 0)
			{
				Alert.showErrorTip("购买成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
			}
		}
	}
}